/**
 * Created by a1 on 16/8/30.
 */

(function($) {
    _initDMLib()
})(jQuery);

function _initDMLib(){
    $.alerts = {
        maskOpacity: 0.4,

        // Private methods
        _show: function(title, message, confirm, callbackConfirm, cancel, callbackCancel) {
            //$.alerts._hide();
            //console.log("confirm = " + confirm + ",callbackConfirm = " + callbackConfirm + ",cancel = " + cancel + ",callbackCancel = " + callbackCancel)
            var body = $("body")
            var nodeMask = $("<div id='dm_lib_popup_mask'></div>")
            nodeMask.css({
                //height: $(document).height(),
                //opacity: $.alerts.maskOpacity
            });
            nodeMask.click(function(){
                $.alerts._hide()
                if(!isEmpty(callbackCancel)){
                    callbackCancel()
                }
            })
            body.append(nodeMask)
            nodeMask.addClass("dm_lib_show")

            var nodeContainer = $("<div id='dm_lib_popup_container'></div>")
            nodeContainer.click(function(){
                $.alerts._hide()
            })
            body.append(nodeContainer)

            if(!isEmpty(title)) {
                var node = $("<div id='dm_lib_popup_title'>"+title+"</div>")
                nodeContainer.append(node)
            }

            var nodeButtonContainer = $("<div id='dm_lib_popup_button_container'></div>")
            nodeButtonContainer.addClass("dm_lib_divider_top")
            if(!isEmpty(message)) {
                var node = $("<div id='dm_lib_popup_msg'>"+message+"</div>")
                nodeContainer.append(node)
            }
            nodeContainer.append(nodeButtonContainer)

            if(isEmpty(confirm)) {
                confirm = "确定"
            }
            var nodeButtonConfirm = $("<div class='dm_lib_popup_button'>"+confirm+"</div>")
            nodeButtonConfirm.click(function(){
                $.alerts._hide()
                if(!isEmpty(callbackConfirm)){
                    callbackConfirm()
                }
            })
            nodeButtonContainer.append(nodeButtonConfirm)
            if(!isEmpty(cancel)) {
                nodeButtonConfirm.addClass("dm_lib_confirm")
                var nodeButtonCancel = $("<div class='dm_lib_popup_button dm_lib_cancel'>"+cancel+"</div>")
                nodeButtonCancel.click(function(){
                    $.alerts._hide()
                    if(!isEmpty(callbackCancel)){
                        callbackCancel()
                    }
                })
                nodeButtonContainer.append(nodeButtonCancel)
            }
            $.alerts._reposition()
            disableScroll()
        },

        _hide: function() {
            $("#dm_lib_popup_mask").remove();
            $("#dm_lib_popup_container").remove();
            enableScroll()
        },

        _reposition: function() {
            var top = (($(window).height() / 2) - ($("#dm_lib_popup_container").outerHeight() / 2));
            var left = (($(window).width() / 2) - ($("#dm_lib_popup_container").outerWidth() / 2));
            //top += $(window).scrollTop()
            if( top < 0 ) top = 0;
            if( left < 0 ) left = 0;

            $("#dm_lib_popup_container").css({
                top: top + 'px',
                left: left + 'px'
            });
            $("#dm_lib_popup_mask").height( $(document).height() );
        },
    }
}

dm_alert = function(title, message, confirm, callbackConfirm, cancel, callbackCancel) {
    $.alerts._show(title, message, confirm, callbackConfirm, cancel, callbackCancel)
}

function loadCSS(filename) {
    var fileref = document.createElement('link');
    fileref.setAttribute("rel","stylesheet");
    fileref.setAttribute("type","text/css");
    fileref.setAttribute("href",filename);
    document.getElementsByTagName("head")[0].appendChild(fileref);
}

//scroll
function disableScroll() {
    document.ontouchmove = function(){
        return false
    }
}

function enableScroll() {
    document.ontouchmove = function(){
        return true
    }
}


/**
 *日期控件
 * @param fmt
 * @returns {*}
 * @constructor
 */
Date.prototype.format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

function format(timestamp){
    return new Date(timestamp).format("yyyy-MM-dd")
}
function dateNow() {
    return new Date().format("yyyy-MM-dd hh:mm:ss")
}

//去空格
function trim(str) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
}

//判断是否为空
function isEmpty(s) {
    return s == null || s == "" || s == undefined
}

//居中
$.fn.centerHorizontal = function(){
    var parentWidth = this.parent().width()
    this.css("left", ((parentWidth - this.outerWidth())*0.5) + "px")
}
$.fn.centerVertical = function(){
    var parentHeight = this.parent().height()
    this.css("top", ((parentHeight - this.outerHeight())*0.5) + "px")
}
$.fn.center = function() {
    this.centerHorizontal()
    this.centerVertical()
}